package com.hongkang.springtrains.web;

import com.hongkang.springtrains.model.User;
import com.hongkang.springtrains.web.exception.UserNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.util.UriComponentsBuilder;

@RestController
@RequestMapping(value = "/do")
public class DoController {

    public DoController(){
        System.out.println("DoController");
    }


    @RequestMapping(value = "/DoA",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public User  DoA(User user){
        System.out.println(user);
        user.setCode(1000L);
        return user;
    }


    @RequestMapping(value = "/DoB")
    public User DoB(User user, UriComponentsBuilder componentsBuilder){
        System.out.println(user);

        System.out.println(componentsBuilder.path("/").build().toUri());
        if(user.getEmail() == null){
          throw new UserNotFoundException("");
        }
        return user;
    }



    @ExceptionHandler(UserNotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public Error userNotFound(UserNotFoundException ex){
        long userID = ex.getUserID();
        return new Error(userID,"User not found by ID"+userID);
    }


}
